/*
 * cm_thread.h
 *
 *  Created on: 2015年12月1日
 *      Author: chens
 */

#ifndef INCLUDE_CM_THREAD_CM_THREAD_H_
#define INCLUDE_CM_THREAD_CM_THREAD_H_

#include <pthread.h>

typedef void *(*routine)(void *);

static inline int thread_create_detach(routine func)
{

    pthread_attr_t attr;
    pthread_t tid;

    if (0 != pthread_attr_init(&attr))
        return -1;

    if (0 != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
        pthread_attr_destroy(&attr);
        return -1;
    }

    if (0 != pthread_create(&tid, &attr, func, NULL)) {
        pthread_attr_destroy(&attr);
        return -1;
    }

    return 0;
}

#endif /* INCLUDE_CM_THREAD_CM_THREAD_H_ */
